package com.traffic.between.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.traffic.between.config.BetweenProperties;

public class XieMeiServiceImpl {
	private static Logger logger = Logger.getLogger(XieMeiServiceImpl.class);
	public static XieMeiServiceImpl service = new XieMeiServiceImpl();

	private static String HOST_ORDER_PATH = BetweenProperties.XMKJ_ORDER_URL;
	private static String HOST_ORDERSTATUS_PATH = BetweenProperties.XMKJ_ORDERSTATUS_URL;
	private static String XMKJ_APPKEY = BetweenProperties.XMKJ_APPKEY;
	private static String XMKJ_SECURITYKEY = BetweenProperties.XMKJ_SECURITYKEY;

	public String order(String productId, String phoneNo, String cstmOrderNo) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("mobileNo", phoneNo);
		params.put("productid", productId);
		params.put("username", XMKJ_APPKEY);
		params.put("password", XMKJ_SECURITYKEY);
		logger.info("bsm- 协美科技流量订购请求时JSON:  " + params);
		StringBuffer resultBuffer = null;
		// 构建请求参数
		StringBuffer sbParams = new StringBuffer();
		if (params != null && params.size() > 0) {
			for (Entry<String, Object> e : params.entrySet()) {
				sbParams.append(e.getKey());
				sbParams.append("=");
				sbParams.append(e.getValue());
				sbParams.append("&");
			}
		}
		HttpURLConnection con = null;
		OutputStreamWriter osw = null;
		BufferedReader br = null;
		// 发送请求
		try {
			URL url = new URL(HOST_ORDER_PATH);
			con = (HttpURLConnection) url.openConnection();
			con.setRequestMethod("POST");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setUseCaches(false);
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			if (sbParams != null && sbParams.length() > 0) {
				osw = new OutputStreamWriter(con.getOutputStream(), "utf-8");
				osw.write(sbParams.substring(0, sbParams.length() - 1));
				osw.flush();
			}
			// 读取返回内容
			resultBuffer = new StringBuffer();
			int contentLength = Integer.parseInt(con.getHeaderField("Content-Length"));
			if (contentLength > 0) {
				br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
				String temp;
				while ((temp = br.readLine()) != null) {
					resultBuffer.append(temp);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (osw != null) {
				try {
					osw.close();
				} catch (IOException e) {
					osw = null;
					throw new RuntimeException(e);
				} finally {
					if (con != null) {
						con.disconnect();
						con = null;
					}
				}
			}
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					br = null;
					throw new RuntimeException(e);
				} finally {
					if (con != null) {
						con.disconnect();
						con = null;
					}
				}
			}
		}

		return resultBuffer.toString();

	}

	public String orderStatus(String cstmOrderNo) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("request_no", cstmOrderNo);

		logger.info("bsm- 协美科技流量订购请求时JSON:  " + params);
		StringBuffer resultBuffer = null;
		// 构建请求参数
		StringBuffer sbParams = new StringBuffer();
		if (params != null && params.size() > 0) {
			for (Entry<String, Object> entry : params.entrySet()) {
				sbParams.append(entry.getKey());
				sbParams.append("=");
				sbParams.append(entry.getValue());
				sbParams.append("&");
			}
		}
		HttpURLConnection con = null;
		BufferedReader br = null;
		try {
			URL url = null;
			if (sbParams != null && sbParams.length() > 0) {
				url = new URL(HOST_ORDERSTATUS_PATH + "?" + sbParams.substring(0, sbParams.length() - 1));
			} else {
				url = new URL(HOST_ORDERSTATUS_PATH);
			}
			con = (HttpURLConnection) url.openConnection();
			con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			con.connect();
			resultBuffer = new StringBuffer();
			br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
			String temp;
			while ((temp = br.readLine()) != null) {
				resultBuffer.append(temp);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					br = null;
					throw new RuntimeException(e);
				} finally {
					if (con != null) {
						con.disconnect();
						con = null;
					}
				}
			}
		}
		return resultBuffer.toString();
	}

}
